/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.common.b;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.DisplayMetrics;
import com.qihoo.gamecenter.sdk.common.i.c;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;

public class a {
    private static int a = 0;
    private static int b = 0;
    private static int[] c;
    private static b d = null;

    public static Drawable a(Context paramContext, String paramString, int paramInt) {
        if (paramInt == 0)
            return null;
        if (b == 0)
            a(paramContext);
        int i = 0;
        int j = 240;
        int k = 0;
        int i2 = 0;
        for (int i3 = 0; i3 < c.length; ++i3) {
            if (c[i3] > 0)
                i = a >> c[i3] * 4;
            else
                i = a << -c[i3] * 4;
            i <<= 16;
            if ((i & paramInt) != i)
                continue;
            int i4 = 0;
            k = paramInt & 0xFFFF;
            if ((i & 0xF000000) == i) {
                i4 = paramInt >> 24;
                j = 320;
            }
            else if ((i & 0xF00000) == i) {
                i4 = paramInt >> 20;
                j = 480;
            }
            else if ((i & 0xF0000) == i) {
                i4 = paramInt >> 16;
                j = 640;
            }
            else {
                i4 = paramInt >> 28;
                j = 240;
            }
            k += (i4 & 0x3);
            i2 = ((i4 & 0x8) == 8) ? 1 : 0;
            break;
        }
        Bitmap localBitmap = (Bitmap) d.a(paramString + k);
        Object localObject1;
        Rect localRect;
        if (localBitmap == null) {
            localObject1 = null;
            localRect = null;
            byte[] arrayOfByte;
            try {
                localObject1 = new DataInputStream(paramContext.getAssets().open(paramString));
                int i5 = k * 4;
                ((DataInputStream) localObject1).skip(i5);
                int l = ((DataInputStream) localObject1).readInt();
                int i1 = ((DataInputStream) localObject1).readInt() - l;
                ((DataInputStream) localObject1).skip(l - (i5 += 8));
                arrayOfByte = new byte[i1];
                int i6 = 0;
                int i7 = 0;
                do {
                    if (-1 == (i6 = ((DataInputStream) localObject1).read(arrayOfByte, i7, i1 - i7)))
                        break;
                    i7 += i6;
                } while (i7 != i1);
                BitmapFactory.Options localOptions = new BitmapFactory.Options();
                localOptions.inTargetDensity = b;
                localOptions.inDensity = j;
                localBitmap = BitmapFactory.decodeByteArray(arrayOfByte, 0, i1, localOptions);
                if (localBitmap == null) {
                    Object localObject3 = null;
                    return localObject3;
                }
                d.a(paramString + k, localBitmap);
            } catch (Exception localException2) {
                localException2.printStackTrace();
                Object localObject2 = null;
                return localObject2;
            } finally {
                try {
                    if (localObject1 != null) {
                        ((DataInputStream) localObject1).close();
                        localObject1 = null;
                    }
                    if (localRect != null) {
                        localRect.close();
                        localRect = null;
                    }
                    arrayOfByte = null;
                } catch (Exception localException5) {
                    c.b("GSDrawable", new Object[] { localException5.toString() });
                }
            }
        }
        if (i2 != 0) {
            localObject1 = localBitmap.getNinePatchChunk();
            localRect = new Rect();
            a(localObject1, localRect);
            return new NinePatchDrawable(paramContext.getResources(), localBitmap, localObject1, localRect, null);
        }
        return ((Drawable) new BitmapDrawable(paramContext.getResources(), localBitmap));
    }

    public static byte[] b(Context paramContext, String paramString, int paramInt) {
        if (b == 0)
            a(paramContext);
        DataInputStream localDataInputStream = null;
        try {
            if (paramInt == 0) {
                Object localObject1 = null;
                return localObject1;
            }
            long l = 0L;
            int i = 0;
            for (int i1 = 0; i1 < c.length; ++i1) {
                if (c[i1] > 0)
                    l = a >> c[i1] * 4;
                else
                    l = a << -c[i1] * 4;
                l <<= 16;
                if ((l & paramInt) != l)
                    continue;
                i2 = paramInt >> 28;
                i = paramInt & 0xFFFF;
                if ((l & 0xF000000) == l)
                    i2 = paramInt >> 24;
                else if ((l & 0xF00000) == l)
                    i2 = paramInt >> 20;
                else if ((l & 0xF0000) == l)
                    i2 = paramInt >> 16;
                i += (i2 & 0x3);
                break;
            }
            localDataInputStream = new DataInputStream(paramContext.getAssets().open(paramString));
            i1 = i * 4;
            localDataInputStream.skip(i1);
            int j = localDataInputStream.readInt();
            int k = localDataInputStream.readInt() - j;
            localDataInputStream.skip(j - (i1 += 8));
            byte[] arrayOfByte1 = new byte[k];
            int i2 = 0;
            int i3 = 0;
            int i4;
            do {
                if (-1 == (i2 = localDataInputStream.read(arrayOfByte1, i3, k - i3)))
                    break;
                i3 += i2;
            } while (i4 != k);
            byte[] arrayOfByte2 = arrayOfByte1;
            return arrayOfByte2;
        } catch (Exception localException1) {
            localException1.printStackTrace();
            Object localObject2 = null;
            return localObject2;
        } finally {
            try {
                if (localDataInputStream != null) {
                    localDataInputStream.close();
                    localDataInputStream = null;
                }
            } catch (Exception localException5) {
                c.b("GSDrawable", new Object[] { localException5.toString() });
            }
        }
    }

    public static void a(Context paramContext) {
        DisplayMetrics localDisplayMetrics = paramContext.getResources().getDisplayMetrics();
        b = localDisplayMetrics.densityDpi;
        switch (b) {
        case 120:
            a = 4194304;
            c = new int[] { 2, 3, 4, 5 };
            break;
        case 160:
            a = 262144;
            c = new int[] { 1, 2, 3, 4 };
            break;
        case 240:
            a = 16384;
            c = new int[] { 0, 1, 2, 3 };
            break;
        case 320:
            a = 1024;
            c = new int[] { 0, -1, 1, 2 };
            break;
        case 480:
            a = 64;
            c = new int[] { 0, -2, -1, 1 };
            break;
        case 640:
            a = 4;
            c = new int[] { 0, -3, -2, -1 };
            break;
        default:
            a = 16384;
            c = new int[] { 0, 2, 1, 3 };
        }
        if (d != null)
            return;
        d = new b((int) Runtime.getRuntime().maxMemory() / 3);
    }

    public static void a(byte[] paramArrayOfByte, Rect paramRect) {
        paramRect.left = a(paramArrayOfByte, 12);
        paramRect.right = a(paramArrayOfByte, 16);
        paramRect.top = a(paramArrayOfByte, 20);
        paramRect.bottom = a(paramArrayOfByte, 24);
    }

    private static int a(byte[] paramArrayOfByte, int paramInt) {
        int i = paramArrayOfByte[(paramInt + 0)];
        int j = paramArrayOfByte[(paramInt + 1)];
        int k = paramArrayOfByte[(paramInt + 2)];
        int l = paramArrayOfByte[(paramInt + 3)];
        int i1 = i | j << 8 | k << 16 | l << 24;
        return i1;
    }
}